package me.lc.rank;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

import me.lc.FunPeaceActivity;
import me.lc.rank.SubmitScoreAsyncTask.Param;
import android.os.AsyncTask;

import com.kuguo.ad.MainActivity;

public class SubmitScoreAsyncTask extends AsyncTask<Param, Integer, Boolean> {
	@Override
	protected Boolean doInBackground(Param... params) {
		if (params != null && params.length > 0) {
			Param param = params[0];
			try {
				String url = String.format(
						"%s%s?id=%s&name=%s&type=%d&score=%d",
						SAEHelper.SAE_SERVER_URL, SAEHelper.SUBMIT_SCORE_URL,
						param.id, java.net.URLEncoder.encode(param.name),
						param.type, param.score);
				URL feedUrl = new URL(url);
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(feedUrl.openConnection()
								.getInputStream()));
				String result = reader.readLine();
				if (result != null
						&& result.toLowerCase().trim().equals("true")) {
					return true;
				}
				return false;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	@Override
	protected void onPostExecute(Boolean result) {
		FunPeaceActivity.Instance.showTip("您累积放屁："
				+ FunPeaceActivity.Instance.piCount + "次, 分数提交"
				+ (result ? "成功" : "失败"));
		SAEHelper.getMyRank(1);
	}

	public class Param {
		public String id;
		public String name;
		public int type;
		public long score;
	}
}
